Skip to content

Fix schema sidecar atomic write#9

Merged
jiangpengcheng merged 3 commits into
mainfrom
codex/fix-schema-sidecar-atomic-write
Jul 14, 2026
Merged

Fix schema sidecar atomic write#9
jiangpengcheng merged 3 commits into
mainfrom
codex/fix-schema-sidecar-atomic-write

Conversation

@jiangpengcheng

Copy link
Copy Markdown
Member

Summary

  • write sink.schema.json through a same-directory temp file and atomic rename
  • clean up temp files on write failures
  • add a regression test that verifies the final sidecar path is atomically replaced instead of written through directly

Testing

  • go test ./...

@jiangpengcheng jiangpengcheng requested a review from a team as a code owner July 8, 2026 09:04
Comment thread pf/schema_sidecar.go
return sidecarPath, nil
}

func writeFileAtomic(path string, content []byte, perm os.FileMode) error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writeFileAtomic gives atomic visibility — readers see either the old file or the fully-written one — but not durability: without tmpFile.Sync() before Close() (and ideally an fsync of the parent dir after Rename), a crash right after Rename can leave the sidecar empty or missing even though the call returned success. For a co-located, startup-regenerated sidecar this is likely fine — just confirming durability isn't a requirement here. If it is, add the syncs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed: this sidecar is regenerated before the function starts, so the requirement is atomic visibility rather than crash durability. I left the sync behavior unchanged.

Comment thread pf/schema_sidecar.go Outdated
}()

_, writeErr := tmpFile.Write(content)
chmodErr := tmpFile.Chmod(perm)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor behavior change worth flagging: os.CreateTemp + Chmod(perm) yields mode 0644 deterministically, whereas the old os.WriteFile(path, content, 0644) produced 0644 &^ umask for a new file and preserved the existing file's mode when the sidecar already existed. The deterministic 0644 is arguably better — flagging only in case a restrictive umask was being relied on.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the atomic writer to preserve the mode of an existing regular sidecar and to let the process umask set the initial mode for a new one. Added a regression test for existing-mode preservation.

@jiangpengcheng jiangpengcheng merged commit 70850f1 into main Jul 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants